/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DataComparisonPanel.java
 *
 * The Panel within Plan Window (PlanFrame) that contains Data Comparisons between existing and new data.
 *
 * --- Last Update: 5/20/2010 12:16 PM ---
 *
 * Update Notes 5/20/2010 12:16 PM by Adrian Wijasa:
 * Added a Save button to write the Data Comparison results into a CSV file.
 *
 * Created on Apr 23, 2010, 7:42 PM
 *
 * CSV Loader
 * Copyright 2007, 2009, 2010 Adrian Wijasa
 *
 * This file is part of CSV Loader.
 *
 * CSV Loader is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * CSV Loader is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with CSV Loader.  If not, see <http://www.gnu.org/licenses/>.
 */

package panels;

import forms.FileChooserFrame;
import forms.Main;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.ListSelectionModel;
import table.StaticTableModel;

/**
 *
 * @author awijasa
 */
public class DataComparisonPanel extends javax.swing.JPanel {

    /** Creates new form DataComparisonPanel */
    public DataComparisonPanel( Main main ) {
        this.main = main;
        initComponents();
        initTable();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        scrollPane = new javax.swing.JScrollPane();
        table = new javax.swing.JTable();
        loadLabel = new javax.swing.JLabel();
        saveLabel = new javax.swing.JLabel();

        table.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        table.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
        table.setColumnSelectionAllowed(true);
        scrollPane.setViewportView(table);

        loadLabel.setFont(new java.awt.Font("Dialog", 1, 11));
        loadLabel.setForeground(new java.awt.Color(0, 102, 255));
        loadLabel.setText("Load Data Comparison (may take a while)");
        loadLabel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                loadLabelMouseClicked(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                loadLabelMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                loadLabelMouseExited(evt);
            }
        });

        saveLabel.setFont(new java.awt.Font("Dialog", 1, 11));
        saveLabel.setForeground(new java.awt.Color(0, 102, 255));
        saveLabel.setText("Save");
        saveLabel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                saveLabelMouseClicked(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                saveLabelMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                saveLabelMouseExited(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                        .addContainerGap()
                        .add(scrollPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 756, Short.MAX_VALUE))
                    .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                        .add(176, 176, 176)
                        .add(loadLabel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 227, Short.MAX_VALUE)
                        .add(101, 101, 101)
                        .add(saveLabel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .add(238, 238, 238)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(scrollPane, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 506, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(saveLabel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(loadLabel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void loadLabelMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_loadLabelMouseEntered
        loadLabel.setForeground( main.black );
    }//GEN-LAST:event_loadLabelMouseEntered

    private void loadLabelMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_loadLabelMouseExited
        loadLabel.setForeground( main.blue );
    }//GEN-LAST:event_loadLabelMouseExited

    private void loadLabelMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_loadLabelMouseClicked
        loadLabel.setForeground( main.red );

        main.planFrame.setTitle( "Plan - Loading..." );
        main.planFrame.setEnabled( false );
        setTableData();
        main.planFrame.setTitle( "Plan" );
        main.planFrame.setEnabled( true );
    }//GEN-LAST:event_loadLabelMouseClicked

    private void saveLabelMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_saveLabelMouseEntered
        saveLabel.setForeground( main.black );
    }//GEN-LAST:event_saveLabelMouseEntered

    private void saveLabelMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_saveLabelMouseExited
        saveLabel.setForeground( main.blue );
    }//GEN-LAST:event_saveLabelMouseExited

    private void saveLabelMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_saveLabelMouseClicked
        saveLabel.setForeground( main.red );
        main.popUpFileChooser( FileChooserFrame.SAVE_DATA_COMPARISON );
    }//GEN-LAST:event_saveLabelMouseClicked

    private void initTable() {
        table.getTableHeader().setReorderingAllowed( false );
        table.setSelectionMode( ListSelectionModel.SINGLE_SELECTION );
    }

    /* Displays the Data Comparisons on JTable */
    private void setTableData() {
        try {
            main.csvPanel.currentImage.plan.buildDataComparison();
            table.setModel( new StaticTableModel( main.get2DVector( main.csvPanel.currentImage.plan.dataComparisonList ), new Vector<String>( main.csvPanel.currentImage.plan.columnTitleList ) ) );
        }
        catch( Exception e ) {
            JOptionPane.showMessageDialog( null, e.getMessage(), "Data Comparison Error", JOptionPane.ERROR_MESSAGE  );
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel loadLabel;
    private javax.swing.JLabel saveLabel;
    private javax.swing.JScrollPane scrollPane;
    private javax.swing.JTable table;
    // End of variables declaration//GEN-END:variables

    private Main main;
}
